import json
import time
import urllib.request
import urllib.parse
from datetime import datetime

def get_universities(page, size=20):
    url = 'https://api.zjzw.cn/web/api/'
    headers = {
        'accept': 'application/json, text/plain, */*',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'content-type': 'application/json',
        'origin': 'https://www.gaokao.cn',
        'referer': 'https://www.gaokao.cn/',
        'sec-ch-ua': '""',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '""',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'cross-site',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15'
    }
    
    data = {
        "keyword": "",
        "page": page,
        "province_id": "",
        "ranktype": "",
        "request_type": 1,
        "signsafe": "c4a764dd13648063647e69b8c262d9bc",
        "size": size,
        "top_school_id": "[589]",
        "type": "",
        "uri": "apidata/api/gkv3/school/lists"
    }
    
    # 将数据转换为JSON字符串
    data_json = json.dumps(data).encode('utf-8')
    
    # 创建请求对象
    req = urllib.request.Request(url, data=data_json, headers=headers, method='POST')
    
    # 发送请求并获取响应
    with urllib.request.urlopen(req) as response:
        response_data = response.read().decode('utf-8')
        
    # 解析JSON响应
    return json.loads(response_data)

def save_to_file(data, filename="大学数据.txt"):
    with open(filename, 'a', encoding='utf-8') as f:
        for school in data.get('data', {}).get('item', []):
            # 保存所有字段
            for key, value in school.items():
                f.write(f"{key}: {value}\n")
            
            # 添加分隔线
            f.write("--------------------------------------------------\n")
            
    # 如果是第一页，保存一份原始JSON数据用于分析字段结构
    if data.get('data', {}).get('current_page', 0) == 1:
        with open("大学数据_原始.json", 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)

# 创建文件并添加头部信息
def initialize_file(filename="大学数据.txt"):
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(f"获取时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
        f.write("==== 中国大学列表 ====\n\n")

def main():
    total_pages = 150  # 总页数
    filename = "大学数据.txt"
    
    # 初始化文件
    initialize_file(filename)
    
    print(f"开始获取大学数据，共计{total_pages}页...")
    
    for page in range(1, total_pages + 1):
        try:
            print(f"正在获取第 {page}/{total_pages} 页数据...")
            result = get_universities(page)
            save_to_file(result, filename)
            
            # 避免请求过快，增加适当延时
            time.sleep(1)
            
        except Exception as e:
            print(f"获取第 {page} 页数据时出错: {str(e)}")
    
    print(f"数据获取完成，已保存到 {filename}")
    print(f"第一页的原始数据已保存到 大学数据_原始.json")

if __name__ == "__main__":
    main()
